package com.shj.em.biz.hr.controller;

import com.shj.em.base.exception.BizException;
import com.shj.em.biz.hr.vo.DeptDropdownVO;
import com.shj.em.biz.sys.entity.Cd;
import com.shj.em.biz.hr.entity.Emp;
import com.shj.em.biz.sys.service.CdService;
import com.shj.em.biz.hr.service.EmpService;
import com.shj.em.common.to.Tree;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;

import com.shj.em.biz.hr.entity.Dept;
import com.shj.em.biz.hr.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import com.shj.em.base.controller.BaseController;


/**
 * <p>
 * 部门信息 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-07-04
 */
@RestController
@Slf4j
@RequestMapping("/hr/dept")
public class DeptController extends BaseController<Dept>{

    @Autowired
    DeptService deptService;

    @Autowired
    EmpService empService;

    @GetMapping("/all")
    public ResMsg<List<Tree<Dept>>> list(){
        Tree<Dept> root = deptService.getDeptTree();
        //去掉顶层的空节点
        return ResMsg.ok(root.getChild());
    }

    @GetMapping("/dropdown")
    public ResMsg<DeptDropdownVO> getDropdownValuesForDept(){
        DeptDropdownVO vo = new DeptDropdownVO();

        List<Cd> effortTypes = cdService.queryCdsByType(CdService.CD_TYPE_EFFORT_TYPE);
        vo.setEffortTypes(effortTypes);

        List<Cd> effortWeights = cdService.queryCdsByType(CdService.CD_TYPE_EFFORT_WEIGHTS);
        vo.setEffortWeights(effortWeights);

        List<Dept> depts = deptService.getAllDepts();
        vo.setAllDepts(depts);
        return ResMsg.ok(vo);
    }

    @GetMapping("/{id}")
    public ResMsg<Dept> getDeptById(@PathVariable("id") Long id){
        return ResMsg.ok(deptService.selectById(id));
    }

	@Log("创建部门信息")
	@PostMapping
	public ResMsg add(@RequestBody Dept dept){
        boolean flag = deptService.isDeptExist(dept.getDeptName());
        if(flag){
            throw new BizException("存在同名部门");
        }
        deptService.insert(dept);
        return ResMsg.ok();
	}

    @Log("更新部门信息")
	@PutMapping
	public ResMsg edit(@RequestBody Dept dept){
        boolean flag = deptService.isDeptExistExclude(dept.getDeptName(), dept.getId());
        if(flag){
            throw new BizException("存在同名部门");
        }
        deptService.updateById(dept);
        return ResMsg.ok();
	}


    @Log("删除部门")
    @DeleteMapping("/{id}")
    public ResMsg remove(@PathVariable("id") Long id){
        List<Emp> emps = empService.findEmpsByDeptId(id);
        if(emps != null && !emps.isEmpty()){
            throw new BizException("该部门下还有员工，请先把员工转到别的部门。");
        }

        List<Dept> depts = deptService.getChildDepts(id);
        if(depts != null && !depts.isEmpty()){
            throw new BizException("请先删除子部门");
        }
        deptService.deleteById(id);
        return ResMsg.ok();
	}

}

